home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Tools / macfreeze / macmodulefinder.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.5 KB  |  108 lines

  1. """macmodulefinder - Find modules used in a script. Only slightly
  2. mac-specific, really."""
  3.  
  4. import sys
  5. import os
  6.  
  7. import directives
  8.  
  9. try:
  10.     # This will work if we are frozen ourselves
  11.     import modulefinder
  12. except ImportError:
  13.     # And this will work otherwise
  14.     _FREEZEDIR=os.path.join(sys.prefix, ':Tools:freeze')
  15.     sys.path.insert(0, _FREEZEDIR)
  16.     import modulefinder
  17.  
  18. #
  19. # Modules that must be included, and modules that need not be included
  20. # (but are if they are found)
  21. #
  22. MAC_INCLUDE_MODULES=['site', 'exceptions']
  23. MAC_MAYMISS_MODULES=['posix', 'os2', 'nt', 'ntpath', 'dos', 'dospath', 
  24.         'win32api',
  25.         'nturl2path', 'pwd', 'sitecustomize']
  26.  
  27. # An exception:
  28. Missing="macmodulefinder.Missing"
  29.  
  30. class Module(modulefinder.Module):
  31.     
  32.     def gettype(self):
  33.         """Return type of module"""
  34.         if self.__path__:
  35.             return 'package'
  36.         if self.__code__:
  37.             return 'module'
  38.         if self.__file__:
  39.             return 'dynamic'
  40.         return 'builtin'
  41.  
  42. class ModuleFinder(modulefinder.ModuleFinder):
  43.  
  44.     def add_module(self, fqname):
  45.         if self.modules.has_key(fqname):
  46.             return self.modules[fqname]
  47.         self.modules[fqname] = m = Module(fqname)
  48.         return m
  49.         
  50. def process(program, modules=[], module_files = [], debug=0):
  51.     error = []
  52.     #
  53.     # Add the standard modules needed for startup
  54.     #
  55.     modules = modules + MAC_INCLUDE_MODULES
  56.     #
  57.     # search the main source for directives
  58.     #
  59.     extra_modules, exclude_modules, extra_path = \
  60.             directives.findfreezedirectives(program)
  61.     for m in extra_modules:
  62.         if os.sep in m:
  63.             # It is a file
  64.             module_files.append(m)
  65.         else:
  66.             modules.append(m)
  67.     # collect all modules of the program
  68.     path = sys.path[:]
  69.     dir = os.path.dirname(program)
  70.     path[0] = dir    # "current dir"
  71.     path = extra_path + path
  72.     #
  73.     # Create the module finder and let it do its work
  74.     #
  75.     modfinder = ModuleFinder(path, 
  76.             excludes=exclude_modules, debug=debug)
  77.     for m in modules:
  78.         modfinder.import_hook(m)
  79.     for m in module_files:
  80.         modfinder.load_file(m)
  81.     modfinder.run_script(program)
  82.     module_dict = modfinder.modules
  83.     #
  84.     # Tell the user about missing modules
  85.     #
  86.     maymiss = exclude_modules + MAC_MAYMISS_MODULES
  87.     for m in modfinder.badmodules.keys():
  88.         if not m in maymiss:
  89.             if debug > 0:
  90.                 print 'Missing', m
  91.             error.append(m)
  92.     #
  93.     # Warn the user about unused builtins
  94.     #
  95.     for m in sys.builtin_module_names:
  96.         if m in ('__main__', '__builtin__'):
  97.             pass
  98.         elif not module_dict.has_key(m):
  99.             if debug > 0:
  100.                 print 'Unused', m
  101.         elif module_dict[m].gettype() != 'builtin':
  102.             # XXXX Can this happen?
  103.             if debug > 0:
  104.                 print 'Conflict', m
  105.     if error:
  106.         raise Missing, error
  107.     return module_dict
  108.